package 线程中断;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-10-08
 * Time: 11:09
 */
public class demo2 {
    public static void main1(String[] args) {
        Thread t = new Thread(){
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()){

                }
                System.out.println("t线程结束");
            }
        };
        t.start();
        t.interrupt();
    }

    public static void main(String[] args) {
        Thread t = new Thread(){
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()){
                    System.out.println("t线程在执行");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();//这里会在触发异常五秒后在中断线程
                        }
                        break;
                    }
                }
                System.out.println("t线程结束");
            }
        };
        t.start();
        System.out.println("设置让t线程结束");
        t.interrupt();
    }
}
